/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.sys.web;

import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.sys.entity.Office;
import com.thinkgem.jeesite.modules.sys.entity.Role;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.service.SystemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 登录Controller
 * @author ThinkGem
 * @version 2013-5-31
 */
@Controller
public class FindPasswordController extends BaseController{
	
	@Autowired
	private SystemService systemService;
	

	/**
	 * 跳转到 找回密码页面
	 */
	@RequestMapping("${adminPath}/password/toFind")
	public String findPassword(){
		return "modules/sys/findPassword";
	}

	@RequestMapping("${adminPath}/password/find")
	public String find(HttpServletRequest request, HttpServletResponse response, String username, Model model, RedirectAttributes redirectAttributes){
        List<User> areaadmins = new ArrayList<>();

        User user = new User();
        user.setName(username);
        List<User> users = systemService.findList(user);

        if(null == users || users.isEmpty()){
            addMessage(redirectAttributes, "用户名或者账号不正确,没有查询到该用户!");
        }else if(users.size() >= 2){
            addMessage(redirectAttributes, "查询到超过两名用户,请使用手机号查询!");
        }else{
            User temuser = users.get(0);
            String enname = temuser.getUserType();
            Office company = temuser.getCompany();
            // 区域管理员忘记密码找管理员
            if("2".equals(enname)){
                User admin = systemService.getUser("u_1");
                areaadmins.add(admin);
            }else if("1".equals(enname)){
                areaadmins.add(temuser);
            }
            // 其他用户忘记密码找 区域管理员
            else{
                User usera = systemService.getUser(temuser.getMaintainBy().getId());
               
                areaadmins.add(usera);
            }
        }

        model.addAttribute("areaadmins", areaadmins);
		return "modules/sys/findPasswordAdmins";
	}

}
